<?php

$oUrl = &App::getUrl();

$oDb = &Database::get();

$oReq = &App::getRequest();

$oTpl = &App::getTemplate();

$iId = $oReq->get('id');

$modules = App::getParam('modules');

$iPage = $oReq->get('page', 1);

$iPageSize = 20;

if ( $oReq->get('save') )
{
	$aPost = $oReq->getArray('text');

	foreach($aPost as $a => $b)
	{
		if ( !empty($b) )
		{
			$aQuery = array
			(
				'text' => $b,
				'time' => time(),
				'version' => App::getParam('info.version'),
				'default' => $b,
				'missing' => '',
			);
			$oDb->update(App::getT('language_phrase'), $aQuery, "phraseid = '". $a ."'");
		}
	}

	$sCache = App::format('Mod_Language.missing_phrases');

	$oLang = &App::getLanguage();
	$oLang->removeCachedFiles();

	if (isset($_SESSION['build']['phrasesValid']))
	{
		App::gotoUrl('admin.language.missing', array('id' => $iId, 'page' => $iPage, 'build' => 'yes'), $sCache, false);
	}
	else
	{
		App::gotoUrl('admin.language.missing', array('id' => $iId, 'page' => $iPage), $sCache, false);
	}
}

$iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('language_phrase') ." AS p
	WHERE `languageid` = '". $iId ."' AND `missing` = '1'");

$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

$aMissingItems = $oDb->getRows("SELECT phraseid, varname, text FROM ". App::getT('language_phrase') ."
	WHERE `languageid` = '". $iId ."' AND `missing` = '1'
	LIMIT ". $iOffset .",". $iPageSize ."
	");

if ( !$iCnt )
{
	$aMissing = array();

	if ( !file_exists(App::getParam('path') . "include/languages/phpfox-language_en.php") )
	{
		trigger_error("Cant load main language package.", E_USER_ERROR);
		exit;
	}

	require_once App::getParam('path') . "include/languages/phpfox-language_en.php";

	$iMissingCount = 0;
	foreach($_MSG as $aItem)
	{
		$iCheck = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('language_phrase') ."
			WHERE `languageid` = '". $iId ."' AND `varname` = '". $aItem['varname'] ."'");

		if ( !$iCheck )
		{
			$iMissingCount++;
			$aQuery = array
			(
				'languageid' => $iId,
				'varname' => $aItem['varname'],
				'text' => $aItem['text'],
				'product' => 'konsort',
				'time' => $aItem['time'],
				'version' => $aItem['version'],
				'module' => $aItem['module'],
				'default' => $aItem['text'],
				'missing' => '1',
			);
			$oDb->insert(App::getT('language_phrase'), $aQuery);
		}
	}

	if ( $iMissingCount )
	{
		$oLang = &App::getLanguage();
		$oLang->removeCachedFiles();
		if ($oReq->get('build'))
		{
			$_SESSION['build']['phrasesValid'] = true;
			App::gotoUrl('admin.language.missing', array('id' => $iId, 'build' => 'yes'));
		}
		else
		{
			App::gotoUrl('admin.language.missing', array('id' => $iId));
		}
	}
}

if ( $oReq->get('build') )
{
	if (!$iCnt)
	{
		$_SESSION['build']['phrases'] = true;
		App::gotoUrl('admin.frames.main');
	}
}

$oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);

$iFrom = $oPager->getOffset($iPage, $iPageSize);
$iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

$oTpl->assign(array
(
	'aPaging'   => $oPager->getInfo($oUrl),
	'aItems' => $aMissingItems,
	'iCnt'             => $iCnt,
	'iTo'              => $iTo,
	'iFrom'            => ($iCnt) ? ($iFrom + 1) : 0,
));

?>